package ca.uwo.android.mustangs;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;
import ca.uwo.android.HomeScreenActivity;
import ca.uwo.android.R;
import ca.uwo.android.rss.RssFeed;
import ca.uwo.android.rss.RssItem;
import ca.uwo.android.rss.events.RssDatesFeed;

public class MustangsMain extends HomeScreenActivity {

	private static final String TAG_NAME = "MustangsMain";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		GridView gridView = (GridView) findViewById(getGridViewId());
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				if (position == 0) {
					startActivityFromChild(MustangsMain.this, new Intent(
							MustangsMain.this, MustangsTickets.class), -1);
				} else if (position == 1) {
					startNewsActivity(getString(R.string.mustangs_scores_link),
							getString(R.string.mustangs_scores_title));
				} else if (position == 2) {
					startNewsActivity(getString(R.string.mustangs_news_link),
							getString(R.string.mustangs_news_title));
				} else if (position == 3) {
					startEventCalendarActivity(
							getString(R.string.mustangs_schedule_link),
							getString(R.string.mustangs_schedules_title),
							RssDatesFeed.MUSTANGSCHEDULE);

				} 
//				else if (position == 4) {
//					openMustangsPhoto(v);
//				}
//				else if (position == 5) {
//					openMustangsVideo(v);
//				} 
				else if (position == 4) {
					startActivityFromChild(MustangsMain.this, new Intent(
							MustangsMain.this, MustangsJoinUs.class), -1);
				} else {
					alert("Not yet implemented.", false);
				}
			}
		});
	}

	/**
	 * Open the Mustangs video of the week
	 * 
	 * @param v
	 */
	private void openMustangsVideo(View v) {

		String feedLocation = null;

		String link = null;

		MustangsVideoParser parser = null;

		RssFeed feed = null;

		RssItem item = null;

		try {

			feedLocation = getString(R.string.mustangs_video_link);
			//Log.i(TAG_NAME, "Pulling feed from:  " + feedLocation);
			
			parser = new MustangsVideoParser(feedLocation);

			feed = parser.parse();

			if (feed == null) {
				handleError("Unable to get video item from feed. Feed is null.");
			} else if (feed.size() == 0) {
				handleError("Unable to get video item from feed. Feed is empty.");
			} else {

				//Log.i(TAG_NAME, "Mustangs video feed found with " + feed.size()
//						+ " items.");

				item = feed.get(0);

				if (item == null)
					handleError("Unable to get video item from feed.  First item is null.");

				else {
					link = item.getLink();

					if (link == null)
						handleError("Unable to show video. Video link from Mustang's feed is null.");

					else {
						// show url in browser
						Intent browser = new Intent(Intent.ACTION_VIEW, Uri
								.parse(link));
						startActivity(browser);
					}
				}
			}
		} catch (Exception e) {
			handleException(e, "Failed to pull video url from feed.");
		}

	}
	
	
	
	/**
	 * Open the Mustangs photo of the week
	 * 
	 * @param v
	 */
	private void openMustangsPhoto(View v) {

		String feedLocation = null;

		String link = null;

		MustangsPhotoParser parser = null;

		RssFeed feed = null;

		RssItem item = null;

		try {

			feedLocation = getString(R.string.mustangs_photo_link);
			//Log.i(TAG_NAME, "Pulling feed from:  " + feedLocation);
			
			parser = new MustangsPhotoParser(feedLocation);

			feed = parser.parse();

			if (feed == null) {
				handleError("Unable to get photo item from feed. Feed is null.");
			} else if (feed.size() == 0) {
				handleError("Unable to get photo item from feed. Feed is empty.");
			} else {

				//Log.i(TAG_NAME, "Mustangs photo feed found with " + feed.size()
//						+ " items.");

				item = feed.get(0);

				if (item == null)
					handleError("Unable to get photo item from feed.  First item is null.");

				else {
					link = item.getLink();

					if (link == null)
						handleError("Unable to show photo. Photo link from Mustang's feed is null.");

					else {
						// show url in browser
						Intent browser = new Intent(Intent.ACTION_VIEW, Uri
								.parse(link));
						startActivity(browser);
					}
				}
			}
		} catch (Exception e) {
			handleException(e, "Failed to pull photo url from feed.");
		}

	}

	/**
	 * Start a date activity
	 * 
	 * @param url
	 *            the url of the RSS dates feed
	 * @param header
	 *            the name that should be put in the screen header
	 * @param calendarType
	 *            the type of calendar this is
	 */
	protected void startEventCalendarActivity(String url, String header,
			int calendarType) {
			Log.d("Jennifer","In mustangsMain");
			Intent intent = new Intent(this,
					ca.uwo.android.rss.events.RssDatesFeed.class);
			intent.putExtra("URL", url);
			intent.putExtra("HEADER", header);
			intent.putExtra("TYPE", calendarType);
			startActivity(intent);
		
		
	}

	@Override
	protected Integer[] getModuleIcons() {
		Integer[] icons = { R.drawable.mustangs_tickets,
				R.drawable.mustangs_scores, 
				R.drawable.mustangs_news,
				R.drawable.mustangs_schedules, 
//				R.drawable.mustangs_photos, 
//				R.drawable.mustangs_videos,
				R.drawable.mustangs_joinus

		};
		return icons;
	}

	@Override
	protected int getContentViewId() {
		return R.layout.mustangs_main;
	}

	@Override
	protected int getGridViewId() {
		return R.id.mustangsGridView;
	}

	@Override
	protected int getModuleNames() {
		return R.array.mustangs_module_name_array;
	}

	

	

}
